//
//  PackageDetailRequest.m
//  BaseProject
//
//  Created by 王洋 on 2021/1/23.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "PackageDetailBannerCell.h"
#import "PackageDetailInfoCell.h"
#import "PackageDetailStepCell.h"
#import "PackageDetailStoreInfoCell.h"
#import "PackageDetailRecommandCell.h"
#import "PackageDetailExplainCell.h"
#import "PackageDetailInfoShowCell.h"
#import "ProductDetailNormalSectionView.h"
#import "PackageDetailSectionHeader.h"


#import "PackageDetailRequest.h"

@implementation PackageDetailRequest

+ (void)request_packageDetailWith:(NSString *)packageId statistics:(BOOL)shouldStatistics comption:(void (^)(BOOL success, PackageDetailModule *model))comption {
    
    PackageDetailModule *model = [[PackageDetailModule alloc] init];

    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:packageId forKey:@"goodsId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    if (shouldStatistics) {
        [dic setValue:@"1" forKey:@"isStatistics"];
    }

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_PackageDetail];
    
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *sysCouponList = [resultDic valueForKey:@"sysCouponList"];
                NSArray *sellerOpusList = [resultDic valueForKey:@"sellerOpusList"];
                NSArray *sellerGoodsList = [resultDic valueForKey:@"sellerGoodsList"];
                
                NSArray *userEvaluateList = [resultDic valueForKey:@"userEvaluateList"];
                NSDictionary *seller = [resultDic valueForKey:@"seller"];
                NSDictionary *sellerGoods = [resultDic valueForKey:@"sellerGoods"];
                NSDictionary *sellerGoodsServiceFlag = [sellerGoods valueForKey:@"sellerGoodsServiceFlag"];

                NSArray *SellerPromiseLabelAuthernticationList = [resultDic valueForKey:@"SellerPromiseLabelAuthernticationList"];
                NSArray *sysWorryFreeLabel = [resultDic valueForKey:@"sysWorryFreeLabel"];

                NSArray *lunboList = [resultDic valueForKey:@"lunboList"];
                NSArray *videofengmianList = [resultDic valueForKey:@"videofengmianList"];

                
                NSArray *imgTextList = [resultDic valueForKey:@"imgTextList"];
                NSArray *videoList = [resultDic valueForKey:@"headvideo"];
                NSDictionary *userCollection = [resultDic valueForKey:@"userCollection"];

                model.userCollection = [CommonFavorModel modelWithDictionary:userCollection];
                
                if (model.userCollection.isDel == 2) {
                    model.isCollect = YES;
                } else {
                    model.isCollect = NO;
                }
                
                model.sellerModel = [CommonSellerModule modelWithDictionary:seller];
                
                if (model.sellerModel.lat > 0 && model.sellerModel.lng > 0) {
                    model.sellerModel.distance = [Utilities calculateStart:CLLocationCoordinate2DMake([TTUserModel sharedObject].latitude, [TTUserModel sharedObject].longitude) end:CLLocationCoordinate2DMake(model.sellerModel.lat,model.sellerModel.lng)];

                }
                
                model.sellerGoodsModel = [CommonPackageModel modelWithJSON:sellerGoods];
                
                //套餐sku赋值
                if ([sellerGoods isKindOfClass:[NSDictionary class]]&&
                    [sellerGoods.allKeys containsObject:@"autoCarSpecsVo"]) {
                    NSDictionary *autoCarSpecsVo = [sellerGoods valueForKey:@"autoCarSpecsVo"];
                    model.sellerGoodsModel.autoCarSpecsVo = [WeddingCarSpecsVo modelWithJSON:autoCarSpecsVo];
                }

                if ([sellerGoods isKindOfClass:[NSDictionary class]]&&
                    [sellerGoods.allKeys containsObject:@"sellerGoodsCarSpecsList"]) {
                    NSArray *sellerGoodsCarSpecsList = [sellerGoods valueForKey:@"sellerGoodsCarSpecsList"];
                    model.sellerGoodsModel.sellerGoodsCarSpecsList = @[].mutableCopy;
                    if ([sellerGoodsCarSpecsList isKindOfClass:[NSArray class]]) {
                        for (NSDictionary *dic in sellerGoodsCarSpecsList) {
                            WeddingCarSpecModel *specModel = [WeddingCarSpecModel modelWithJSON:dic];
                            [model.sellerGoodsModel.sellerGoodsCarSpecsList addObject:specModel];
                        }
                    }
                }
                
                model.sellerGoodsServiceFlag = [CommonServiceContentModel modelWithDictionary:sellerGoodsServiceFlag];
                model.couponArray = @[].mutableCopy;
                if ([sysCouponList isKindOfClass:[NSArray class]]) {
                    for (int i=0; i < sysCouponList.count; i++) {
                        NSDictionary *sysCouponListDic = sysCouponList[i];
                        CommonCouponModel *couponModel = [CommonCouponModel modelWithDictionary:sysCouponListDic];
                        [model.couponArray addObject:couponModel];
                    }
                }
                
                model.sellerGoodsList = @[].mutableCopy;
                if ([sellerGoodsList isKindOfClass:[NSArray class]]) {
                    for (int i=0; i < sellerGoodsList.count; i++) {
                        NSDictionary *sellerGoodsListDic = sellerGoodsList[i];
                        CommonPackageModel *couponModel = [CommonPackageModel modelWithJSON:sellerGoodsListDic];
                        [model.sellerGoodsList addObject:couponModel];
                    }
                }
                
                model.evaluateArray = @[].mutableCopy;
                if ([userEvaluateList isKindOfClass:[NSArray class]]) {
                    for (int i=0; i < userEvaluateList.count; i++) {
                        NSDictionary *userEvaluateListDic = userEvaluateList[i];
                        
                        CommonEvaluateModel *evaluateModel = [CommonEvaluateModel modelWithDictionary:userEvaluateListDic];
                        
                        NSArray *imageUrls = [userEvaluateListDic valueForKey:@"imageUrls"];
                        if ([imageUrls isKindOfClass:[NSArray class]]) {
                            evaluateModel.imageUrls = @[].mutableCopy;
                            for (int j = 0; j < imageUrls.count; j++) {
                                CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imageUrls[j]];
                                [evaluateModel.imageUrls addObject:imgModel];
                            }
                        }
                        
                        evaluateModel.infoHeight = 60.0f;
                        evaluateModel.evaluateTextHeight = [Utilities getStringHeightWithText:evaluateModel.ueEvaluateContent font:[UIFont systemFontOfSize:12] viewWidth:TTScreenWidth-30];
                        NSInteger lineCount = evaluateModel.imageUrls.count/3;
                        if (evaluateModel.imageUrls.count%3>0) {
                            lineCount++;
                        }
                        evaluateModel.collectionViewHeight = lineCount * ((TTScreenWidth - 30)/3);
                        evaluateModel.thumbHeight = 40.0f;
                        evaluateModel.itemHeight = evaluateModel.infoHeight + evaluateModel.evaluateTextHeight + evaluateModel.collectionViewHeight + evaluateModel.thumbHeight;
                        [model.evaluateArray addObject:evaluateModel];
                    }
                }
                
                model.sellerOpusList = @[].mutableCopy;
                if ([sellerOpusList isKindOfClass:[NSArray class]]) {
                    for (int i=0; i < sellerOpusList.count; i++) {
                        NSDictionary *sellerOpusListDic = sellerOpusList[i];
                        CommonOpusModel *opusModel = [CommonOpusModel modelWithDictionary:sellerOpusListDic];
                        [model.sellerOpusList addObject:opusModel];
                    }
                }
                
                model.SellerPromiseLabelAuthernticationList = @[].mutableCopy;
                if ([SellerPromiseLabelAuthernticationList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < SellerPromiseLabelAuthernticationList.count; i++) {
                        NSDictionary *SellerPromiseLabelAuthernticationListDic = SellerPromiseLabelAuthernticationList[i];
                        CommonPromiseTagModel *tagmodel = [CommonPromiseTagModel modelWithDictionary:SellerPromiseLabelAuthernticationListDic];
                        [model.SellerPromiseLabelAuthernticationList addObject:tagmodel];
                    }
                }
                
                model.videoList = @[].mutableCopy;
                if ([videoList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < videoList.count; i++) {
                        NSDictionary *videoListDic = videoList[i];
                        CommonMediaModel *mediaModel = [CommonMediaModel modelWithDictionary:videoListDic];
                        if (i == 0 && mediaModel.url.length > 0) {
                            model.videoUrl = mediaModel.url;
                        }
                        [model.videoList addObject:mediaModel];
                    }
                }
                
                model.sysWorryFreeLabel = @[].mutableCopy;
                if ([sysWorryFreeLabel isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sysWorryFreeLabel.count; i++) {
                        NSDictionary *sysWorryFreeLabelDic = sysWorryFreeLabel[i];
                        CommonWYXTagModel *labModel = [CommonWYXTagModel modelWithDictionary:sysWorryFreeLabelDic];
                        [model.sysWorryFreeLabel addObject:labModel];
                    }
                }
                
                model.lunboList = @[].mutableCopy;
                model.bannerPics = @[].mutableCopy;
                if ([lunboList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < lunboList.count; i++) {
                        NSDictionary *lunboListDic = lunboList[i];
                        CommonMediaModel *mediaModel = [CommonMediaModel modelWithDictionary:lunboListDic];
                        
                        if (mediaModel.url.length > 0) {
                            
                            NSString *resizeUrl = [TT_OSSManager reSourceUrl:mediaModel.url type:BA_resizeType_webP];
                            [TT_OSSManager cacheSourceUrl:resizeUrl];
                            [model.bannerPics addObject:resizeUrl];
//                            [TT_OSSManager cacheSourceUrl:mediaModel.url];
                        }
                        [model.lunboList addObject:mediaModel];
                    }
                }
                
                model.videofengmianList = @[].mutableCopy;
                if ([videofengmianList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < videofengmianList.count; i++) {
                        NSDictionary *videofengmianListDic = videofengmianList[i];
                        CommonMediaModel *mediaModel = [CommonMediaModel modelWithDictionary:videofengmianListDic];
                        [model.videofengmianList addObject:mediaModel];
                    }
                }

                model.imgTextList = @[].mutableCopy;
                model.images = @[].mutableCopy;
                if ([imgTextList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < imgTextList.count; i++) {
                        NSDictionary *imgTextListDic = imgTextList[i];
                        CommonMediaModel *mediaModel = [CommonMediaModel modelWithDictionary:imgTextListDic];
                        if (mediaModel.url.length > 0) {
                            NSString *resizeUrl = [TT_OSSManager reSourceUrl:mediaModel.url type:BA_resizeType_webP];
                            [TT_OSSManager cacheSourceUrl:resizeUrl];
                            [model.images addObject:resizeUrl];
                        }
                        [model.imgTextList addObject:mediaModel];
                    }
                }
                        
                CGFloat mediaBannerHeight = 0;
                model.package_y = mediaBannerHeight;
                CGFloat bannerHeight = [PackageDetailBannerCell calculateHeight];
                CGFloat infoHeight = [PackageDetailInfoCell calculateHeight];
                CGFloat explanHeight = [PackageDetailExplainCell calculateHeight];
                
                CGFloat orderGiftHeight = [PackageDetailInfoShowCell calculateHeight];
                if (model.sellerGoodsModel.goodsDepositGift.length == 0) {
                    orderGiftHeight = CGFLOAT_MIN;
                }
                
                CGFloat allPayGiftHeight = [PackageDetailInfoShowCell calculateHeight];
                if (model.sellerGoodsModel.goodsTotalGift.length == 0) {
                    allPayGiftHeight = CGFLOAT_MIN;
                }
                
                CGFloat stepHeight = [PackageDetailStepCell calculateHeight];

                model.eva_y = bannerHeight + infoHeight + explanHeight + orderGiftHeight + allPayGiftHeight + stepHeight- kStatusBarHeight - NavBar_Height;

                CGFloat evaHeight = CGFLOAT_MIN;
                if (model.evaluateArray.count > 0) {
                    evaHeight =([ProductDetailNormalSectionView calculateHeight] + model.evaluateArray.firstObject.itemHeight) ;
                }
                CGFloat storeInfoHeight = [PackageDetailStoreInfoCell calculateHeightWithModel:model];
                CGFloat storeRecHeight = CGFLOAT_MIN;
                CGFloat storeRecHeadHeight = [ProductDetailNormalSectionView calculateHeight];;
                if (model.sellerGoodsList.count > 0) {
                    NSInteger line = model.sellerGoodsList.count/2;
                    NSInteger yu = model.sellerGoodsList.count%2;
                    if (yu > 0) {
                        line = line + 1;
                    }
                    if (line > 2) {
                        line = 2;
                    }
                    CGFloat lineHeight = line * [PackageDetailRecommandCell calculateHeight] + ((line - 1) * 15);
                    storeRecHeight = lineHeight + 20 + storeRecHeadHeight;//20为组间距
                }
                
                CGFloat packageDetailHeadHeight = [PackageDetailSectionHeader calculateHeight];;
                
                model.detail_y = model.eva_y + evaHeight + storeInfoHeight + storeRecHeight + packageDetailHeadHeight + 5;
                comption(YES,model);
                
            } else {
                
                comption(NO,nil);
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
        } else {
            
            comption(NO,nil);
            [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
        comption(NO,nil);

    }];
    
    
}

@end
